import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'bookM',

  state: {
    pageIsChange: false, // 记录页面有没有删除或新增成功
    mapData: [], // 记录所有的簿记
    titleOpen: {
      isOpen1: true, //基础期权信息标题是否展开
      isOpen2: true, //香草期权标题是否展开
      isOpen3: true, //障碍期权标题是否展开
      isOpen4: true, //二元期权标题是否展开
      isOpen7: true, //雪球期权标题是否展开
    },
    dictionary: {}, //所有字典枚举
    onlyView: false, // 只查看不编辑
  },
  effects: {
    //获取表格信息
    *getBookData({ payload, callback }, { call, put }) {
      const response = yield call(api.book.getOptionDetail, payload);
      if (response && response.code === '0') {
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(response?.msg || '获取表格信息失败');
      }
    },
    // 保存一条簿记-新增
    *saveOneBook({ payload, callback }, { call, put }) {
      const response = yield call(api.book.saveOption, payload);
      if (response && response.code === '0') {
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(response.msg || '保存失败');
      }
    },
    // 跟新一条簿记-新增
    *upDataOneBook({ payload, callback }, { call, put }) {
      const response = yield call(api.book.updateOption, payload);
      if (response && response.code == '0') {
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(response.msg || '更新失败');
      }
    },
    // 删除一条簿记
    *deleteOneBook({ payload, callback }, { call, put }) {
      const response = yield call(api.book.deleteOptionByBookId, payload);
      if (response && response.code === '0') {
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(response.msg || '删除失败');
      }
    },
    // 产品组名列表
    *getProductNameList({ payload, callback }, { call, put }) {
      const response = yield call(api.getProducts, payload);
      if (response && response.success) {
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(response.errorMsg || '产品组查询失败');
      }
    },
    // 交易对手getTradingCompany
    *getCounterpartyList({ payload, callback }, { call, put }) {
      const response = yield call(api.getTradingCompany, payload);
      if (response && response.success) {
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(response.errorMsg || '交易对手查询失败');
      }
    },
    // 证券代码
    *getUnderlyingList({ payload, callback }, { call, put }) {
      const response = yield call(api.getSecuritiesList.all, payload);
      if (response && response.code === 0) {
        callback('success', response.data.dataSource);
      } else {
        callback('fail');
        message.error(response.errorMsg || '证券编码查询失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 标题和内容展开
    changeOpenStatus(state, { payload }) {
      let titleOpen = state.titleOpen;
      return {
        ...state,
        titleOpen: { ...titleOpen, ...payload },
      };
    },
  },
};
